/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multi.agent.simulation.behaviour;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import multi.agent.simulation.BestRoadReply;
import multi.agent.simulation.BestRoadRequest;
import multi.agent.simulation.MovingAgent;
import multi.agent.simulation.entity.Rutas;

/**
 *
 * @author Mauro Perez
 */
public class MovingRequest extends Behaviour {
    private byte _step = 0;
    private MovingAgent _agent;
    private String _origin;
    private String _destination;
    private final String _language = "English";
    private final String _ontology = "traffic-ontology";
    private boolean _denied = false;
    private BestRoadReply _bestReply = null;
    
    
    public MovingRequest(MovingAgent agent, String origin, String destination) {
        _agent = agent;
        _origin = origin;
        _destination = destination;
    }
    
    @Override
    public void action() {
        
        ACLMessage msg = _agent.receive();
        
        if(msg != null) 
        {
            switch(msg.getPerformative()) 
            {
                case ACLMessage.INFORM:
                       
                    BestRoadReply reply = BestRoadReply.FromMessageString(msg.getContent());
                    if (_bestReply == null || _bestReply.Weight > reply.Weight) 
                    {
                        _bestReply = reply;
                        System.out.println("Best Road Found - " + _agent._agentName + ": " + msg.getContent());
                        //send statistic
                        System.out.println("RegisterStaticts ....");
                        _agent.SendStatistic(_origin,_destination,"response");
                        MovingToDestiny(_bestReply);
                    }
                    break;
                    
                default:
                    break;
            }
        }
    }
    
    private void MovingToDestiny(BestRoadReply reply)
    {
        String origing = reply.Road.get(0);
        String destiny = "";
        System.out.println("Start Moving from  - " + origing);
        
        for (int i = 0; i < reply.Road.size(); i++)
        {
            try 
            {
                String node = reply.Road.get(i);
                System.out.println("Moving To  - " + node);
                destiny = node;
                Thread.sleep(1000); //to simulate traffic in the road. 1 second
            } 
            catch (InterruptedException ex) 
            {
                Logger.getLogger(MovingRequest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        System.out.println("Done, Destiny Reached - " + destiny);
        System.out.println("Completed Road From  - " + origing + " To - "  + destiny);
        
    }

    @Override
    public boolean done() {
        return _denied;
    }
}
